package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.domain.entity.*;
import com.example.exception.FloraException;

//import example.Exception.PlantAdminException;
import com.example.mapper.PlantMapper;


import com.example.service.IPlantService;

//import example.Exception.PlantAdminException;
import com.example.mapper.PlantMapper;
import com.example.domain.entity.Plant;

import com.example.service.IPlantService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zrr
 * @since 2022-12-01
 */

@Service
public class PlantServiceImpl extends ServiceImpl<PlantMapper, Plant> implements IPlantService {

    @Autowired
    private UploadService uploadService;

    @Override
    public void updateLikeCount(Long plantId) throws FloraException {
        Plant plant = this.baseMapper.selectById(plantId);
        plant.setLikeCount(plant.getLikeCount()+1);
        try {
            this.baseMapper.updateById(plant);
        } catch (Exception e) {
            throw new FloraException("更新点赞量失败");
        }
    }

    @Override
    public IPage<Plant> getPlants(Map<String, Object> condition, Integer pageNum, Integer pageSize) {
        IPage<Plant> page = new Page<>(pageNum,pageSize);
        QueryWrapper<Plant> qw = new QueryWrapper();
        //上架的商品才能看到
        //qw.eq("status",0);

        if(condition.containsKey("plant_name")){
            qw.eq("plant_name",condition.get("plant_name"));
        }
        if(condition.containsKey("category_id")) {
            qw.eq("category_id", condition.get("category_id"));
        }

        Page<Plant> page1 = (Page<Plant>) this.baseMapper.getPlantPage(page,qw);
        //System.out.println(page1.getRecords());
        return page1;
    }

    @Override
    public IPage<Plant> getPlantsByUserId(long userId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Plant> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Plant::getCreateBy,userId);
        Page<Plant> page=new Page<>(pageNum,pageSize);
        this.baseMapper.selectPage(page,lqw);
        return page;
    }

    @Override
    public void deletePlant(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public List<Plant> getHotPlants() {
        return this.baseMapper.getHotPlantList();
//        Page<Plant> page=new Page<>(pageNum,pageSize);
//        LambdaQueryWrapper<Plant> lqw=new LambdaQueryWrapper<>();
//        lqw.orderByDesc(Plant::getFollowerNum);
//        this.baseMapper.selectPage(page,lqw);
//        return page;
    }

    @Override
    public IPage<Plant> getNewPlants(Integer pageNum, Integer pageSize) {
       Page<Plant> page=new Page<>(pageNum,pageSize);
       LambdaQueryWrapper<Plant> lqw=new LambdaQueryWrapper<>();
       lqw.orderByDesc(Plant::getCreateTime);
       this.baseMapper.selectPage(page,lqw);
       return page;
    }

    @Override
    public Plant addPlant(Plant plant) throws FloraException {
        try{
            this.baseMapper.insert(plant);
            return plant;

        }catch (Exception e){
            throw new FloraException("添加植物失败"+e.getMessage());
        }
    }

    @Override
    public void updatePlant(long plantId, Plant plant) throws FloraException {
        if(this.baseMapper.selectById(plantId)==null){
            throw new FloraException("植物"+plantId+"不存在");
        }
        try{
            this.baseMapper.updateById(plant);
        }
        catch (Exception e){
            throw new FloraException("更新植物失败"+e.getMessage());
        }
    }

    @Override
    public String uploadPhotoOfPlant(MultipartFile img, long plantId) throws FloraException {
        try{
           String url= uploadService.uploadImg(img);
           Plant plant=new Plant();
           plant.setPlantId(plantId);
           plant.setAvatar(url);
           this.baseMapper.updateById(plant);
           return url;
        }
        catch (Exception e){
            throw new FloraException("上传植物照片失败"+e.getMessage());
        }
    }

    @Override
    public Plant getPlantById(Long plantId) {
        return baseMapper.selectById(plantId);
    }
}
